package com.itheima.tlais_web_management.service.impl;

import com.itheima.tlais_web_management.mapper.DeptMapper;
import com.itheima.tlais_web_management.mapper.EmpMapper;
import com.itheima.tlais_web_management.pojo.Dept;
import com.itheima.tlais_web_management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

//@Conditional():使用 Conditional 或其 衍生注解 按照一定条件进行判断，只有满足条件时，才可以注册对应的 Bean 到 IOC容器 当中。
@Service
public class DeptServiceImpl implements DeptService {

    //Service中操作数据库得调用Mapper中的方法
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> list() {

        return deptMapper.list();

    }


    //spring 事务管理
    //底层逻辑是 AOP
    //propagation = Propagation.REQUIRES_NEW 事务路径，当我们不希望事务之间相互影响时
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public void delete(Integer id) throws Exception {
        //根据 部门id删除部门
        deptMapper.delete(id);


        //根据 部门id删除员工
        empMapper.deletebydeptid(id);
    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public int updateDepartment(Dept dept) {

        return deptMapper.updateDepartment(dept);

    }

    @Override
    public Dept updatelist(Integer id) {
        return deptMapper.updatelist(id);
    }


}
